package com.example.testdemo.db.sql;

import com.example.testdemo.annotation.Table;
import com.example.testdemo.db.reflex.Reflex;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author 永健
 * @since 2020-03-28 16:57
 */
public class SqlPare<T> {

    private Class<T> t;
    private String tbName;

    public SqlPare(Class<T> t) throws Exception {
        this.t = t;
        Table table = t.getAnnotation(Table.class);
        if (table == null) {
            throw new Exception("构造出错,请在实体类中添加注解 @Table");
        }
        this.tbName = table.name();
    }

    public String deleteByIdSql(Serializable id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("delete ")
                .append(" from ")
                .append(" ")
                .append(tbName)
                .append(" ")
                .append("where")
                .append(" ")
                .append("id")
                .append("=")
                .append(id);
        return buffer.toString();
    }


    public String selectAllSql() {
        List<String> propertis = Reflex.getPropertis(this.t);
        StringBuffer buffer = new StringBuffer();
        buffer.append("select ").append(listToString(propertis)).append(" ").append(" from ").append(" ").append(tbName);
        return buffer.toString();
    }

    public String deleteAll() {
        List<String> strings = Reflex.getPropertis(this.t);
        StringBuffer buffer = new StringBuffer();
        buffer.append("delete ").append(" from ").append(" ").append(tbName);
        return buffer.toString();
    }

    public String insertSql() {

        List<String> strings = Reflex.getPropertiesAndValuesIsNotNull(this.t);

        StringBuffer buffer = new StringBuffer();
        buffer.append("insert");
        buffer.append(" into ");
        buffer.append(" " + tbName + " ");
        buffer.append(" ( ");
        buffer.append(strings.get(0));
        buffer.append(" ) ");

        buffer.append(" values ");
        buffer.append(" ( ");
        buffer.append(strings.get(1));
        buffer.append(" ) ");

        String sql = buffer.toString();
        return sql;
    }

    public String updateByIdSql(Object object) {

        Map<String, Object> keyValues = Reflex.getKeyValues(object);

        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("update");
        sqlBuffer.append(" " + tbName + " ");
        sqlBuffer.append(" set ");

        StringBuffer values = new StringBuffer();
        for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
            values.append(",")
                    .append(" ")
                    .append(entry.getKey())
                    .append(" = ")
                    .append(" ")
                    .append("\"")
                    .append(entry.getValue())
                    .append("\"")
                    .append(" ");
        }
        String substring = values.toString().substring(1);
        sqlBuffer.append(substring);
        sqlBuffer
                .append(" where ")
                .append(" id ")
                .append(" = ")
                .append("\"")
                .append(keyValues.get("id"))
                .append("\"");
        return sqlBuffer.toString();
    }

    public String selectByIdSql() {
        List<String> propertis = Reflex.getPropertis(t);
        StringBuffer buffer = new StringBuffer();
        buffer.append("select ").append(listToString(propertis));
        buffer.append(" ").append("from").append(" ").append(tbName).append(" where ").append(" id ").append(" = ").append("?");

        return buffer.toString();
    }

    private String listToString(List<String> list) {
        StringBuffer buffer = new StringBuffer();
        for (String s : list) {
            buffer.append(",")
                    .append(s);
        }
        return buffer.toString().substring(1);
    }


}